<?php

namespace app\minichakra\controller\v1;

use app\common\cachemodel\AppConfig;
use app\common\cachemodel\chakra\ChakraCrystalGoods;
use app\common\cachemodel\chakra\ChakraCrystalGoodsType;
use app\minichakra\controller\ChakraAuth;

class Crystal extends ChakraAuth
{
    public function index()
    {
        $search = trim(input('search/s', ''));
        $tid = input('tid/d', 0);

        $chakraCrystalGoodsTypeModel = new ChakraCrystalGoodsType();
        $chakraCrystalGoodsModel = new ChakraCrystalGoods();
        $goodsTypeData = $chakraCrystalGoodsTypeModel->getGoodTypeData();
        foreach ($goodsTypeData as $k => $item)
        {
            $isSelected = $item['id'] === $tid ? 1 : 0;
            if (empty($search) && $tid === 0 && $k === 0)
            {
                $isSelected = 1;
            }
            $typeList[] = [
                'title' => $item['name'],
                'tid' => (int)$item['id'],
                'is_selected' => $isSelected,
            ];
        }
        if ($search)
        {
            $typeList = ['title' => '全部', 'tid' => 0, 'is_selected' => $tid === 0 ? 1 : 0];
            foreach ($goodsTypeData as $k => $item)
            {
                $isSelected = $item['id'] === $tid ? 1 : 0;
                $typeList[] = [
                    'title' => $item['name'],
                    'tid' => (int)$item['id'],
                    'is_selected' => $isSelected,
                ];
            }
            $gids = $chakraCrystalGoodsModel->where('status', 1)->where('name', 'like', '%' . $search . '%')->order('sort desc,id desc')->column('id');
        } else
        {
            $typeList = [];
            foreach ($goodsTypeData as $k => $item)
            {
                $isSelected = $item['id'] === $tid ? 1 : 0;
                if ($tid === 0 && $k === 0)
                {
                    $isSelected = 1;
                }
                $typeList[] = [
                    'title' => $item['name'],
                    'tid' => (int)$item['id'],
                    'is_selected' => $isSelected,
                ];
            }

            $gids = $chakraCrystalGoodsModel->getListIds();
        }
        $goodList = $gids ? $chakraCrystalGoodsModel->batchGetInfoByIds($gids) : [];
        $dataList = [];
        foreach ($gids as $gidItem)
        {
            $goodItem = $goodList[$gidItem];
            $dataList[] = [
                'img' => $goodItem['img'],
                'title' => $goodItem['name'],
                'id' => $goodItem['id'],
                'tids' => $goodItem['tids'],
            ];
        }

        $resData = [
            'type_list' => $typeList,
            'data_list' => $dataList,
            'service' => $this->getService(),
        ];

        return show(config('code.success'), '请求成功', $resData);
    }

    public function detail()
    {
        $id = input('id/d', 0);
        if (!$id)
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $chakraCrystalGoodsModel = new ChakraCrystalGoods();
        $goodInfo = $chakraCrystalGoodsModel->getInfoById($id);
        if (!$goodInfo || $goodInfo['status'] !== 1)
        {
            return show(config('code.error'), '服务异常，水晶不存在～');
        }

        $resData = [
            'title' => $goodInfo['name'],
            'content' => $goodInfo['content'],
            'service' => $this->getService(),
        ];

        return show(config('code.success'), '请求成功', $resData);
    }

    protected function getService()
    {
        switch ($this->headers['app-type'])
        {
            case 'WEAPP':   //  微信 小程序
                // 读取后台配置，chakra_config 的 wx_corp_id  wx_im_url
                // 相关文档：https://work.weixin.qq.com/nl/act/p/a733314375294bdd
                // https://developers.weixin.qq.com/miniprogram/dev/api/open-api/service-chat/wx.openCustomerServiceChat.html
                $chakraConfig = (new AppConfig())->configInfo('chakra_config');
                $service = [
                    'text' => $chakraConfig['wx_corp_id'], // 企业微信ID
                    'img' => $chakraConfig['wx_im_url'], // 接待的客服url
                ];
                break;
            case 'TT':  //  抖音 小程序
                // 读取后台配置，chakra_config 的 tt_im_id
                $chakraConfig = (new AppConfig())->configInfo('chakra_config');
                $service = [
                    'text' => $chakraConfig['tt_im_id'] ?? '',
                    'img' => '',
                ];
                break;
            default:    //  H5
                $service = [
                    'text' => '保存扫码 购买好物',
                    'img' => '/test/chakra/qrcode.png',
                ];
        }

        return $service;
    }
}
